﻿
var l_profileId = 0;
var l_destination = "";
var l_name = "";
var indx = 0;

/****************************************************
funciton: createProfile
parameters: destination, firstname, lastname, email
*****************************************************/
function createProfile() {

    $.ajax({
        url: 'Class/insertProfile.aspx',
        data: { destination: $("#txtDestination")[0].value, name: $("#txtName")[0].value, email: $("#txtEmail")[0].value },
        success: function (data) {
            // set the profile id to a local, which used by other functions within the class
            l_profileId = data.result;
            l_destination = $("#txtDestination")[0].value;
            l_name = $("#txtName")[0].value;

            //*** when result is great than 0 which means profile has been created successful
            if (l_profileId > 0) {
            // getIPLocation then create entry of where user coming from
                getIPLocation();
            }

        },
        dataType: "json",
        error: function (xhr, text, err) {
            alert(text)
        }
        });
}

/****************************************************
funciton: getIPLocation
*****************************************************/
function getIPLocation() {
    EasyjQuery_Get_IP("my_callbackAddress", "full"); // full version
//    var boardingPass = [
//                    '<li id="boarding">',
//					'<article>',
//						'<div class="logowhite"></div>',
//						'<div class="success">',
//							'<h3>We\'ve received your request,' + l_name + '</h3>',
//							'<p>Watch your email for an invitation so you can start planning your dream trip to ' + l_destination + '!</p>',
//						'</div>',
//						'<p class="followtwitter">Follow Sillk on twitter for more updates and to get in touch</p>',
//						'<h4 class="twitter"><a href="http://twitter.com/gosillk" id="twitter" class="twitterlink">gosillk</a></h4>',
//					'</article>',
//					'</li>',
//                ].join('');

//    if (indx == 0) {
//        $('.container').data('plugin_curtain').insert({
//            html: boardingPass,
//            htmlId: 'boarding',
//            htmlClass: 'cover',
//            insertAfter: '', // If null, the content is inserted at the end
//            goTo: true // Go to the new panel directly after the insertion (default: true)
//        });

//        indx = 2;
//    }
}

/****************************************************
funciton: my_callbackAddress
*****************************************************/
function my_callbackAddress(json) {
    // more information at http://api.easyjquery.com/test/demo-ip.php
    //alert("IP :" + json.IP + " \nCOUNTRY: " + json.Country + " City: " + json.CityName + " regionName: " + json.RegionName);

    //string ip, string countryCode, string countryName, string regionName, string city, string latitude, string longitude, string continentName, Int32 profileId
    $.ajax({
        url: 'Class/insertProfileLocationInfo.aspx',
        data: { ip: json.IP,
            countryCode: json.CountryCode3,
            countryName: json.CountryName,
            regionName: json.RegionName,
            city: json.CityName,
            latitude: json.CityLatitude,
            longitude: json.CityLongitude,
            continentName: json.ContinentName,
            profileId: l_profileId
        },
        success: function (data) {

            //*** when result is true which means the profilelocation has created successfully.
            // then create boarding pass slide with dest and name div tag
            if (data.result > 0) {
                var boardingPass = [
                    '<li id="boarding">',
					'<article>',
						'<div class="logowhite"></div>',
						'<div class="success">',
							'<h3>We\'ve received your request,' + l_name + '</h3>',
							'<p>Watch your email for an invitation so you can start planning your dream trip to ' + l_destination + '!</p>',
						'</div>',
						'<p class="followtwitter">Follow Sillk on twitter for more updates and to get in touch</p>',
						'<h4 class="twitter"><a href="http://twitter.com/gosillk" id="twitter" class="twitterlink">gosillk</a></h4>',
					'</article>',
					'</li>',
                ].join('');

                if (indx == 0) {
                    $('.container').data('plugin_curtain').insert({
                        html: boardingPass,
                        htmlId: 'boarding',
                        htmlClass: 'cover',
                        insertAfter: '', // If null, the content is inserted at the end
                        goTo: true // Go to the new panel directly after the insertion (default: true)
                    });

                    indx = 2;
                }
            }
        },
        dataType: "json",
        error: function (xhr, text, err) {
            alert(text)
        }
    });
}